package com.ideal.frame.nodecertificate.controller;

import com.ideal.frame.common.dto.ResponseModel;
import com.ideal.frame.nodecertificate.dto.CertificateForm;
import com.ideal.frame.nodecertificate.entity.NodeCertificate;
import com.ideal.frame.nodecertificate.service.NodeCertificateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: liudingsheng
 * @Date: 2021/9/3
 * @Description:
 */
@RestController
@RequestMapping("/certificate")
public class NodeCertificateController {

    @Autowired
    private NodeCertificateService nodeCertificateService;


    @PostMapping("/page")
    public ResponseModel page(@RequestBody CertificateForm certificateForm) {
        return ResponseModel.success(nodeCertificateService.getPage(certificateForm));
    }

    @PostMapping("/createCer")
    public ResponseModel createCer(@RequestBody NodeCertificate nodeCertificate) throws Exception {
        nodeCertificateService.createCer(nodeCertificate);
        return ResponseModel.success();
    }

    @GetMapping("/downLoadPublicKey")
    public void downLoadPublicKey(HttpServletResponse response) throws Exception {
        nodeCertificateService.downLoadTrust(response);
    }

    @GetMapping("/downLoadCer/{id}")
    public void downLoadCer(HttpServletResponse response,@PathVariable Long id) throws IOException {
        nodeCertificateService.downLoadCer(id,response);
    }

    @DeleteMapping("/deleteCer/{id}")
    public ResponseModel deleteCer(@PathVariable Long id) {
        nodeCertificateService.deleteCer(id);
        return ResponseModel.success();
    }


}
